﻿#include "pcrActionBar.h"

PCRActionBar::PCRActionBar(QWidget *parent)
{
    this->setParent(parent);
}

void PCRActionBar::init(PCRState *state){

    // 界面初始化

    pState = state;
    _layout = new QHBoxLayout(this);
    this->setLayout(_layout);
    _layout->setSpacing(10);

    // 页面选择
    _comboBox_page = new QComboBox(this);
    _comboBox_page->setStyleSheet("border-radius: 4px;\ncolor: rgb(255, 255, 255);\nborder: 1px solid #999999;\nbackground: rgb(14,114,212);");
    _comboBox_page->setMinimumWidth(130);
    _comboBox_page->setMinimumHeight(40);
    _comboBox_page->addItem("原始曲线");
    _comboBox_page->addItem("扩增曲线");
    _comboBox_page->addItem("标准曲线");
    _comboBox_page->addItem("下游操作建议");
    _layout->addWidget(_comboBox_page);
    connect(_comboBox_page, SIGNAL(activated(int)), this, SLOT(on_comboBoxPage_changed()));


    // 指导意见
    _guideBar = new PCRActionBarGuide(pState, this);
    _layout->addWidget(_guideBar);


    // 通道筛选
    for(auto iter = pState->channelList.begin(); iter != pState->channelList.end(); iter ++){

        _checkBox_channelList.append(new QCheckBox(this));
        _checkBox_channelList.back()->setText(*iter);
        _checkBox_channelList.back()->setChecked(true);
        QString style;
        if     (*iter == "FAM") style = "QCheckBox {  \n    color: rgb(66, 46, 205); \n}  \n  \nQCheckBox::indicator:checked {  \n    background-color: #422ECD; \n}";
        else if(*iter == "VIC") style = "QCheckBox {  \n    color: rgb(0, 196, 115); \n}  \n  \nQCheckBox::indicator:checked {  \n    background-color: #00C473; \n}";
        else if(*iter == "ROX") style = "QCheckBox {  \n    color: rgb(255, 135, 62); \n}  \n  \nQCheckBox::indicator:checked {  \n    background-color: #FF873E; \n}";
        else if(*iter == "CY5") style = "QCheckBox {  \n    color: rgb(255, 43, 55); \n}  \n  \nQCheckBox::indicator:checked {  \n    background-color: #FF2B37; \n}";

        _checkBox_channelList.back()->setStyleSheet(style);
        _layout->addWidget(_checkBox_channelList.back());
        connect(_checkBox_channelList.back(), SIGNAL(clicked()), this, SLOT(on_checkBoxChannel_clicked()));

        _lineEdit_channelList.append(new QLineEdit(this));
        _layout->addWidget(_lineEdit_channelList.back());
        _lineEdit_channelList.back()->setMaximumWidth(80);
        connect(_lineEdit_channelList.back(), SIGNAL(editingFinished()), this, SLOT(on_lineEditChannel_editingFinished()));

    }

    _layout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding));


    // Chart选项
    _checkBox_Rn = new QCheckBox(this);
    _checkBox_Rn->setText("Rn");
    _checkBox_Rn->setStyleSheet("color: rgb(255, 255, 255);");
    _layout->addWidget(_checkBox_Rn);

    _checkBox_Log = new QCheckBox(this);
    _checkBox_Log->setText("对数");
    _checkBox_Log->setStyleSheet("color: rgb(255, 255, 255);");
    _layout->addWidget(_checkBox_Log);

    _checkBox_Norm = new QCheckBox(this);
    _checkBox_Norm->setText("DIFF");
    _checkBox_Norm->setStyleSheet("color: rgb(255, 255, 255);");
    _layout->addWidget(_checkBox_Norm);
    if(!pState->channelList_filter.contains(pState->modelIR)){       // 内标未设置时禁用归一化
        _checkBox_Norm->setChecked(false);
    }

    _checkBox_Thr = new QCheckBox(this);
    _checkBox_Thr->setText("显示阈值线");
    _checkBox_Thr->setStyleSheet("color: rgb(255, 255, 255);");
    _layout->addWidget(_checkBox_Thr);


    // 筛选
    _pushButton_filter = new QPushButton("筛选", this);
    _pushButton_filter->setMinimumWidth(110);
    _pushButton_filter->setMinimumHeight(38);
    _pushButton_filter->setStyleSheet("color: rgb(255, 255, 255);\nbackground: rgba(0,0,0,0);\nborder: 1px solid white; \ncolor: white; ");
    _layout->addWidget(_pushButton_filter);


    connect(_pushButton_filter, SIGNAL(clicked()), this, SLOT(on_pushButtonFilter_clicked()));
    connect(_checkBox_Rn, SIGNAL(clicked()), this, SLOT(on_checkBoxRn_clicked()));
    connect(_checkBox_Log, SIGNAL(clicked()), this, SLOT(on_checkBoxLog_clicked()));
    connect(_checkBox_Norm, SIGNAL(clicked()), this, SLOT(on_checkBoxNorm_clicked()));
    connect(_checkBox_Thr, SIGNAL(clicked()), this, SLOT(on_checkBoxThr_clicked()));

    // 鼠标悬停事件
    _checkBox_Thr->setMouseTracking(true);


}
QString color2str(QColor color)
{
    QString str = "";
    str = QString::number(color.red())+","+QString::number(color.green())+","+QString::number(color.blue());
    return str;
}
void PCRActionBar::on_colorChanged(){
    if(ChartColorType == 0){
        for(int i = 0; i < _checkBox_channelList.length(); i ++){
            if(pState->channelList.size()<=i || !accessColorList.contains(pState->channelList[i])){
                qDebug() << "ERROR - ActionBar Set CheckBox Color Failed (type0)";
                return;
            }
            QString style = QString("QCheckBox {  \n    color: rgb(%1); \n}  \n  \nQCheckBox::indicator:checked {  \n    background-color: rgb(%2); \n}")
                    .arg(color2str(accessColorList[pState->channelList[i]]), color2str(accessColorList[pState->channelList[i]]));
            qDebug() << "newStyle:" << style;
            _checkBox_channelList[i]->setStyleSheet(style);
        }
    }else{

        QString style1 = "QCheckBox {  \n    color: rgb(66, 46, 205); \n}  \n  \nQCheckBox::indicator:checked {  \n    background-color: #422ECD; \n}";
        QString style2 = "QCheckBox {  \n    color: rgb(0, 196, 115); \n}  \n  \nQCheckBox::indicator:checked {  \n    background-color: #00C473; \n}";
        QString style3 = "QCheckBox {  \n    color: rgb(255, 135, 62); \n}  \n  \nQCheckBox::indicator:checked {  \n    background-color: #FF873E; \n}";
        QString style4 = "QCheckBox {  \n    color: rgb(255, 43, 55); \n}  \n  \nQCheckBox::indicator:checked {  \n    background-color: #FF2B37; \n}";

        if(_checkBox_channelList.size() < 4){
            qDebug() << "ERROR - ActionBar Set CheckBox Color Failed (type1)";
            return;
        }
        _checkBox_channelList[0]->setStyleSheet(style1);
        _checkBox_channelList[1]->setStyleSheet(style2);
        _checkBox_channelList[2]->setStyleSheet(style3);
        _checkBox_channelList[3]->setStyleSheet(style4);
    }

}


void PCRActionBar::on_comboBoxPage_changed(){
    if(_comboBox_page->currentText() == "原始曲线"){
        pState->curPage = PCRState::RAW;
    }else if(_comboBox_page->currentText() == "扩增曲线"){
        pState->curPage = PCRState::AMPL;
        if(_checkBox_Norm->isChecked()){
            pState->curChart = PCRState::NORM;
        }else if(_checkBox_Log->isChecked() && _checkBox_Rn->isChecked()){
            pState->curChart = PCRState::LOG_RN;
        }else if(_checkBox_Log->isChecked()){
            pState->curChart = PCRState::LOG_DELTA_RN;
        }else if(_checkBox_Rn->isChecked()){
            pState->curChart = PCRState::RN;
        }else{
            pState->curChart = PCRState::DELTA_RN;
        }
    }else if(_comboBox_page->currentText() == "标准曲线"){
        pState->curPage = PCRState::STANDARD;
    }else{

        if(pState->needReCalculateGuideData){
            pState->emitGuideRecalculate();
            pState->needReCalculateGuideData = false;
        }
        pState->curPage = PCRState::GUIDE_DATA;

    }

    pState->emitPageChanged();
}

void PCRActionBar::on_checkBoxRn_clicked(){
    _checkBox_Rn->update();
    if(_checkBox_Rn->isChecked()){
       // _checkBox_Norm->setChecked(false);

    }else{
        //_checkBox_Thr->hide();
    }
    if(pState->modelCt < 0){
        pState->needReCalculateGuideData = true;
    }

    changeAmplPageState();
}

void PCRActionBar::on_checkBoxLog_clicked(){
    this->update();
    if(_checkBox_Log->isChecked()){
        _checkBox_Norm->setChecked(false);
        pState->curChart = PCRState::LOG_RN;
    }
    changeAmplPageState();
}

void PCRActionBar::on_checkBoxThr_clicked(){


    if(pState->modelCt >= 0){
        DockLabel::I().show("阈值线显示仅在绝对阈值生效", (QWidget*)this->parent());
        _checkBox_Thr->setChecked(false);
        return;
    }

    if(_checkBox_Thr->isChecked()){
        pState->showThr = true;
    }else{
        pState->showThr = false;
    }

    changeAmplPageState();
}

void PCRActionBar::on_checkBoxNorm_clicked(){
    if(!pState->channelList_filter.contains(pState->modelIR)){       // 内标未设置时禁用归一化
        DockLabel::I().show("DIFF仅在内标有效设置时可用", (QWidget*)this->parent());
        _checkBox_Norm->setChecked(false);
        return;
    }
    if(_checkBox_Norm->isChecked()){
        _checkBox_Log->setChecked(false);
        //_checkBox_Rn->setChecked(false);
    }

    changeAmplPageState();
}

void PCRActionBar::changeAmplPageState(){


    if(_comboBox_page->currentText() != "扩增曲线" && _comboBox_page->currentText() != "标准曲线"){
        return;
    }

    if(_checkBox_Norm->isChecked()){
        pState->curChart = PCRState::NORM;
    }else if(_checkBox_Rn->isChecked() && _checkBox_Log->isChecked()){
        pState->curChart = PCRState::LOG_RN;
    }else if(_checkBox_Log->isChecked()){
        pState->curChart = PCRState::LOG_DELTA_RN;
    }else if(_checkBox_Rn->isChecked()){
        pState->curChart = PCRState::RN;
    }else{
        pState->curChart = PCRState::DELTA_RN;
    }

    pState->emitPageChanged();

}


void PCRActionBar::on_pushButtonFilter_clicked(){
    pState->emitFilterStart();
}


void PCRActionBar::on_checkBoxChannel_clicked(){
    pState->channelList_filter.clear();
    for(auto iter = _checkBox_channelList.begin(); iter != _checkBox_channelList.end(); iter ++){
        if((*iter)->isChecked()){
            pState->channelList_filter.append((*iter)->text());
        }
    }

    pState->emitFilterChanged();
}


void PCRActionBar::on_lineEditChannel_editingFinished(){

    for(int i = 0; i < _lineEdit_channelList.size(); i ++){
        if(pState->curChart == PCRState::DELTA_RN){
            pState->thrList[_checkBox_channelList[i]->text()] = _lineEdit_channelList[i]->text().toDouble();
            pState->thrList_log[_checkBox_channelList[i]->text()] = log10(pState->thrList[_checkBox_channelList[i]->text()]);
        }else if(pState->curChart == PCRState::LOG_DELTA_RN){
            pState->thrList_log[_checkBox_channelList[i]->text()] = _lineEdit_channelList[i]->text().toDouble();
            pState->thrList[_checkBox_channelList[i]->text()] = pow(10, pState->thrList_log[_checkBox_channelList[i]->text()]);
        }else if(pState->curChart == PCRState::NORM){
            pState->thrList_diff[_checkBox_channelList[i]->text()] = _lineEdit_channelList[i]->text().toDouble();
        }else if(pState->curChart == PCRState::RN){
            pState->thrList_Rn[_checkBox_channelList[i]->text()] = _lineEdit_channelList[i]->text().toDouble();
            pState->thrList_logRn[_checkBox_channelList[i]->text()] = log10(pState->thrList_Rn[_checkBox_channelList[i]->text()]);
        }else if(pState->curChart == PCRState::LOG_RN){
            pState->thrList_logRn[_checkBox_channelList[i]->text()] = _lineEdit_channelList[i]->text().toDouble();
            pState->thrList_Rn[_checkBox_channelList[i]->text()] = pow(10, pState->thrList_logRn[_checkBox_channelList[i]->text()]);
        }
    }

    pState->needReCalculateGuideData = true;
    pState->emitThrChanged();
}



// 外部信号处理

void PCRActionBar::on_pageChanged(){

    if(!pState->showThr){
        for(auto iter = _lineEdit_channelList.begin(); iter != _lineEdit_channelList.end(); iter ++){
            (*iter)->hide();
        }
    }

    if(pState->curPage == PCRState::RAW){       // 原始曲线
        _checkBox_Rn->hide();
        _checkBox_Log->hide();
        _checkBox_Thr->hide();
        _checkBox_Norm->hide();
        _guideBar->hide();
        foreach(QCheckBox* box, _checkBox_channelList){
            box->show();
        }
        for(auto iter = _lineEdit_channelList.begin(); iter != _lineEdit_channelList.end(); iter ++){
            (*iter)->hide();
        }
    }else if(pState->curPage == PCRState::GUIDE_DATA || pState->curPage == PCRState::GUIDE_PLAN){   // 指导意见
        _checkBox_Rn->hide();
        _checkBox_Log->hide();
        _checkBox_Thr->hide();
        _checkBox_Norm->hide();
        _guideBar->show();
        _guideBar->refresh();
        foreach(QCheckBox* box, _checkBox_channelList){
            box->hide();
        }
        for(auto iter = _lineEdit_channelList.begin(); iter != _lineEdit_channelList.end(); iter ++){
            (*iter)->hide();
        }
    }else if(pState->curPage == PCRState::STANDARD){    // 标准曲线
        if(pState->modelCt < 0){
            _checkBox_Rn->show();
            _checkBox_Log->show();
            _checkBox_Norm->show();
        }else{
            _checkBox_Rn->hide();
            _checkBox_Log->hide();
            _checkBox_Norm->hide();
        }
        _checkBox_Thr->hide();
        _guideBar->hide();
        foreach(QCheckBox* box, _checkBox_channelList){
            box->show();
        }
        for(auto iter = _lineEdit_channelList.begin(); iter != _lineEdit_channelList.end(); iter ++){
            (*iter)->hide();
        }
    }else{                          // 扩增曲线
        _checkBox_Rn->show();
        _checkBox_Log->show();
        _checkBox_Thr->show();
        _checkBox_Norm->show();
        _guideBar->hide();
        foreach(QCheckBox* box, _checkBox_channelList){
            box->show();
        }
        if(pState->showThr && pState->modelCt < 0){
            for(int i = 0; i < _lineEdit_channelList.size(); i ++){

                if(false && pState->modelIR == _checkBox_channelList[i]->text()){
                    _lineEdit_channelList[i]->hide();
                    // 取消内标的阈值线无法修改
                }else{
                    _lineEdit_channelList[i]->show();
                }
            }
        }else{
            for(auto iter = _lineEdit_channelList.begin(); iter != _lineEdit_channelList.end(); iter ++){
                (*iter)->hide();
            }
        }
    }

    // 阈值线显示更新
    if(pState->curChart == PCRState::LOG_DELTA_RN){
        for(int i = 0; i < _lineEdit_channelList.size(); i ++){
            _lineEdit_channelList[i]->setText(QString::number(pState->thrList_log[_checkBox_channelList[i]->text()]));
        }
    }else if(pState->curChart == PCRState::DELTA_RN){
        for(int i = 0; i < _lineEdit_channelList.size(); i ++){
            _lineEdit_channelList[i]->setText(QString::number(pState->thrList[_checkBox_channelList[i]->text()]));
        }
    }else if(pState->curChart == PCRState::NORM){
        for(int i = 0; i < _lineEdit_channelList.size(); i ++){
            _lineEdit_channelList[i]->setText(QString::number(pState->thrList_diff[_checkBox_channelList[i]->text()]));
        }
    }

}

void PCRActionBar::on_filterChanged(){

}


void PCRActionBar::on_thrChanged(){

    if(pState->curChart == PCRState::LOG_DELTA_RN){
        for(int i = 0; i < _lineEdit_channelList.size(); i ++){
            _lineEdit_channelList[i]->setText(QString::number(pState->thrList_log[_checkBox_channelList[i]->text()]));
        }
    }else if(pState->curChart == PCRState::DELTA_RN){
        for(int i = 0; i < _lineEdit_channelList.size(); i ++){
            _lineEdit_channelList[i]->setText(QString::number(pState->thrList[_checkBox_channelList[i]->text()]));
        }
    }else if(pState->curChart == PCRState::NORM){
        for(int i = 0; i < _lineEdit_channelList.size(); i ++){
            _lineEdit_channelList[i]->setText(QString::number(pState->thrList_diff[_checkBox_channelList[i]->text()]));
        }
    }else if(pState->curChart == PCRState::RN){
        for(int i = 0; i < _lineEdit_channelList.size(); i ++){
            _lineEdit_channelList[i]->setText(QString::number(pState->thrList_Rn[_checkBox_channelList[i]->text()]));
        }
    }else if(pState->curChart == PCRState::LOG_RN){
        for(int i = 0; i < _lineEdit_channelList.size(); i ++){
            _lineEdit_channelList[i]->setText(QString::number(pState->thrList_logRn[_checkBox_channelList[i]->text()]));
        }
    }
}

void PCRActionBar::on_modelChanged(){
    if(pState->modelCt >= 0){
        pState->showThr = false;
        _checkBox_Thr->setChecked(false);
    }else{
    }
}

void PCRActionBar::on_guideNumChanged(){
    _guideBar->refresh();
}
